//
//  SWEventPicker.swift
//  ChildStory
//
//  Created by linhan on 2016/11/21.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWEventPicker: UIView,UIPickerViewDataSource,UIPickerViewDelegate,ScrollTabBarDelegate,SWAPIComponent
{
    private var _selectedRow:Int = -1
    
    var cancelCallback:(()->Void)?
    var chooseCallback:((SWKidEvent)->Void)?
    
//    var selectedEventID:String = ""{
//        didSet{
//            if events.count > 0
//            {
//                let index:Int = events.index(where: {($0.eventID ?? "") == selectedEventID}) ?? -1
//                //_pickview.selectRow(index, inComponent: 0, animated: false)
//            }
//        }
//    }
    
    //var events:[SWKidEvent] = []
    
    private var _selectedStageIndex:Int = 0
    private var _events:[SWKidEvent] = []
    
    lazy private var _pickview:UIPickerView = UIPickerView()
    lazy private var _titleView:UIView = UIView()
    lazy private var _confirmBtn:UIButton = UIButton(type: .system)
    
    private var _tabBar:ScrollTabBar?
    
    var selectedEvent:SWKidEvent?{
        return _events.valueAt(_selectedRow)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show(eventID:String)
    {
        
    }
    
    private func setup()
    {
        backgroundColor = UIColor.white
        
        let titleViewHeight:CGFloat = 30
        _titleView.frame = CGRectMake(0, 0, width, titleViewHeight)
        _titleView.backgroundColor = UIColor(white: 0.92, alpha: 1)
        //addSubview(_titleView)
        
        let cancelBtn:UIButton = UIButton(type: .system)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.sizeToFit()
        cancelBtn.addTarget(self, action: #selector(cancelBtnTapped), for: .touchUpInside)
        _titleView.addSubview(cancelBtn)
        
        _confirmBtn.setTitle("确定", for: .normal)
        _confirmBtn.sizeToFit()
        _confirmBtn.addTarget(self, action: #selector(confirmBtnTapped), for: .touchUpInside)
        _confirmBtn.frame = CGRectMake(width - _confirmBtn.width, 0, _confirmBtn.width, _confirmBtn.height)
        _titleView.addSubview(_confirmBtn)
        
        _tabBar = ScrollTabBar(frame: CGRectMake(0, 0, width, 29))
        _tabBar?.font = UIFont.systemFont(ofSize: 14)
        _tabBar?.itemSpacing = 30
        _tabBar?.alignment = .center
        _tabBar?.selectedColor = UIColor.darkGray
        _tabBar?.unselectedColor = UIColor.lightGray
        _tabBar?.delegate = self
        addSubview(_tabBar!)
        
        _pickview.frame = CGRectMake(0, titleViewHeight, width, height - titleViewHeight)
        _pickview.dataSource = self
        _pickview.delegate = self
        _pickview.backgroundColor = UIColor(hex: 0xf9f9f9, alpha: 1)
        addSubview(_pickview)
        
        loadData()
    }
    
    private func loadData()
    {
        let stages = SWConfigManager.sharedManager().stages
        let items:[String] = stages.map({$0.title ?? ""})
        _tabBar?.titleItems = items
        _tabBar?.selectedIndex = 0
        _events = stages.valueAt(0)?.events?.flatMap({$0 as? SWKidEvent}) ?? []
        _pickview.reloadAllComponents()
    }
    
    //MARK ================================= UIPickerViewDataSource =========================
    
    // returns the number of 'columns' to display.
    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }
    
    //返回每个component的行数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return _events.count
    }
    
    //返回每个component对应行的标题
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return _events.valueAt(row)?.title
    }
    
    //设定标题颜色
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?
    {
        if let title = self.pickerView(pickerView, titleForRow:row, forComponent:component)
        {
            let dic = [NSForegroundColorAttributeName:UIColor.darkGray]
            let attri:NSMutableAttributedString = NSMutableAttributedString(string: title, attributes: dic)
            return attri
        }
        return nil
    }
    
    //选中行
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if component == 0
        {
            _selectedRow = row
            //_pickview.reloadComponent(1)
            //_pickview.selectRow(0, inComponent: 1, animated: true)
        }
        
    }
    
    //MARK ================================= ScrollTabBarDelegate =========================
    func scrollTabBar(_ tabBar: ScrollTabBar, didSelectIndex index: Int)
    {
        let stages = SWConfigManager.sharedManager().stages
        _selectedStageIndex = index
        _events = stages.valueAt(_selectedStageIndex)?.events?.flatMap({$0 as? SWKidEvent}) ?? []
        _pickview.reloadAllComponents()
    }
    
    @objc private func cancelBtnTapped()
    {
        cancelCallback?()
    }
    
    @objc private func confirmBtnTapped()
    {
        //let stages = SWConfigManager.sharedManager().stages
        //chooseCallback?(_selectedRow)
    }
    
    
}
